package com.maodou.core.utils

import android.util.Log
import com.maodou.core.BuildConfig

/**
 * 统一日志工具类
 * 提供分级日志输出，支持Debug/Release环境控制
 */
object Logger {
    
    private const val DEFAULT_TAG = "Logger"
    
    /**
     * 是否启用日志输出
     * Release版本默认关闭，Debug版本默认开启
     */
    var isLoggingEnabled: Boolean = BuildConfig.DEBUG
    
    /**
     * 全局日志标签前缀
     */
    var globalTagPrefix: String = DEFAULT_TAG
    
    /**
     * 构建完整的日志标签
     */
    private fun buildTag(tag: String?): String {
        return if (tag.isNullOrEmpty()) {
            globalTagPrefix
        } else {
            "$globalTagPrefix-$tag"
        }
    }
    
    /**
     * Verbose级别日志
     */
    fun v(message: String, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.v(buildTag(tag), message)
        }
    }
    
    fun v(message: String, throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.v(buildTag(tag), message, throwable)
        }
    }
    
    /**
     * Debug级别日志
     */
    fun d(message: String, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.d(buildTag(tag), message)
        }
    }
    
    fun d(message: String, throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.d(buildTag(tag), message, throwable)
        }
    }
    
    /**
     * Info级别日志
     */
    fun i(message: String, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.i(buildTag(tag), message)
        }
    }
    
    fun i(message: String, throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.i(buildTag(tag), message, throwable)
        }
    }
    
    /**
     * Warning级别日志
     */
    fun w(message: String, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.w(buildTag(tag), message)
        }
    }
    
    fun w(message: String, throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.w(buildTag(tag), message, throwable)
        }
    }
    
    fun w(throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.w(buildTag(tag), throwable)
        }
    }
    
    /**
     * Error级别日志
     */
    fun e(message: String, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.e(buildTag(tag), message)
        }
    }
    
    fun e(message: String, throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.e(buildTag(tag), message, throwable)
        }
    }
    
    fun e(throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.e(buildTag(tag), "", throwable)
        }
    }
    
    /**
     * What a Terrible Failure级别日志
     * 用于记录不应该发生的错误
     */
    fun wtf(message: String, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.wtf(buildTag(tag), message)
        }
    }
    
    fun wtf(message: String, throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            Log.wtf(buildTag(tag), message, throwable)
        }
    }
    
    fun wtf(throwable: Throwable?, tag: String? = null) {
        if (isLoggingEnabled) {
            throwable?.let { Log.wtf(buildTag(tag), it) }
        }
    }
    
    /**
     * 打印JSON格式的日志（格式化输出）
     */
    fun json(jsonString: String, tag: String? = null) {
        if (isLoggingEnabled) {
            try {
                // 简单的JSON格式化（可以后续集成更专业的JSON格式化库）
                val formattedJson = jsonString.replace(",", ",\n")
                    .replace("{", "{\n")
                    .replace("}", "\n}")
                d("JSON: \n$formattedJson", tag)
            } catch (e: Exception) {
                d("JSON (raw): $jsonString", tag)
            }
        }
    }
    
    /**
     * 网络请求日志
     */
    fun network(url: String, method: String, response: String? = null, tag: String? = null) {
        if (isLoggingEnabled) {
            val networkTag = "Network"
            d("[$method] $url", networkTag)
            response?.let {
                d("Response: $it", networkTag)
            }
        }
    }
    
    /**
     * 性能监控日志
     */
    fun performance(operation: String, timeMs: Long, tag: String? = null) {
        if (isLoggingEnabled) {
            i("Performance: $operation took ${timeMs}ms", "Performance")
        }
    }
    
    /**
     * 便捷的性能测量方法
     */
    inline fun <T> measureTime(operation: String, tag: String? = null, block: () -> T): T {
        val startTime = System.currentTimeMillis()
        val result = block()
        val endTime = System.currentTimeMillis()
        performance(operation, endTime - startTime, tag)
        return result
    }
}